package com.data.modules.online.config.service.p020a;

import java.util.List;
import com.data.common.util.ConvertUtils;
import com.data.modules.online.cgform.extend.ExtendJsonKey;
import com.data.modules.online.cgform.util.CgformUtil;
import com.data.modules.online.cgform.util.OnlFormShowType;
import com.data.modules.online.config.executor.ColumnMeta;
import com.data.modules.online.config.executor.DataBaseConst;
import com.data.modules.online.config.service.DbTableHandleI;

public class DbTableDB2HandleImpl implements DbTableHandleI {
    @Override
    public String getAddColumnSql(ColumnMeta columnMeta) {
        String str = " ADD " + columnMeta.getColumnName() + " " + getDataTypeByColunmType(columnMeta);
        if (!ConvertUtils.isNotEmpty(columnMeta.getFieldDefault())) {
            return str;
        }
        String str2 = str + " DEFAULT " + columnMeta.getFieldDefault();
        if (!"Y".equals(columnMeta.getIsNullable())) {
            return str2 + " NOT NULL";
        }
        return str2;
    }

    @Override
    public String getMatchClassTypeByDataType(String dataType, int digits) {
        String lowerCase = dataType.toLowerCase();
        char c = 65535;
        switch (lowerCase.hashCode()) {
            case -1325958191:
                if (lowerCase.equals("double")) {
                    c = 5;
                    break;
                }
                break;
            case 3026845:
                if (lowerCase.equals("blob")) {
                    c = '\b';
                    break;
                }
                break;
            case 3076014:
                if (lowerCase.equals(OnlFormShowType.f320d)) {
                    c = 1;
                    break;
                }
                break;
            case 3560141:
                if (lowerCase.equals("time")) {
                    c = 2;
                    break;
                }
                break;
            case 55126294:
                if (lowerCase.equals("timestamp")) {
                    c = 3;
                    break;
                }
                break;
            case 236613373:
                if (lowerCase.equals(DataBaseConst.f477l)) {
                    c = 0;
                    break;
                }
                break;
            case 1542263633:
                if (lowerCase.equals(DataBaseConst.f470e)) {
                    c = 6;
                    break;
                }
                break;
            case 1958052158:
                if (lowerCase.equals("integer")) {
                    c = 4;
                    break;
                }
                break;
            case 2003998041:
                if (lowerCase.equals("long varchar")) {
                    c = 7;
                    break;
                }
                break;
        }
        switch (c) {
            case 0:
                return DataBaseConst.f474i;
            case 1:
            case 2:
            case 3:
                return OnlFormShowType.f320d;
            case 4:
                return "int";
            case 5:
                return "double";
            case 6:
                return "bigdecimal";
            case 7:
                return ExtendJsonKey.f91e;
            case '\b':
                return "blob";
            default:
                return DataBaseConst.f474i;
        }
    }

    @Override
    public String dropTableSQL(String tableName) {
        return " DROP TABLE  " + tableName.toLowerCase() + " ";
    }

    @Override
    public String getDropColumnSql(String fieldName) {
        return " DROP COLUMN " + fieldName.toUpperCase() + "";
    }

    /**
     * 根据列类型返回数据库字段类型
     * @param aVar
     * @return
     */
    private String getDataTypeByColunmType(ColumnMeta aVar) {
        String lowerCase = aVar.getColunmType().toLowerCase();
        char c = 65535;
        switch (lowerCase.hashCode()) {
            case -1325958191:
                if (lowerCase.equals("double")) {
                    c = 3;
                    break;
                }
                break;
            case -891985903:
                if (lowerCase.equals(DataBaseConst.f474i)) {
                    c = 0;
                    break;
                }
                break;
            case 104431:
                if (lowerCase.equals("int")) {
                    c = 2;
                    break;
                }
                break;
            case 3026845:
                if (lowerCase.equals("blob")) {
                    c = 6;
                    break;
                }
                break;
            case 3076014:
                if (lowerCase.equals(OnlFormShowType.f320d)) {
                    c = 1;
                    break;
                }
                break;
            case 3556653:
                if (lowerCase.equals(ExtendJsonKey.f91e)) {
                    c = 5;
                    break;
                }
                break;
            case 2075457105:
                if (lowerCase.equals("bigdecimal")) {
                    c = 4;
                    break;
                }
                break;
        }
        switch (c) {
            case 0:
                return String.format("varchar(%s)", Integer.valueOf(aVar.getColumnSize()));
            case 1:
                return "TIMESTAMP";
            case 2:
                return "INTEGER";
            case 3:
                return "double";
            case 4:
                return String.format("DECIMAL(%s, %s)", Integer.valueOf(aVar.getColumnSize()), Integer.valueOf(aVar.getDecimalDigits()));
            case 5:
                return "LONG VARCHAR";
            case 6:
                return "BLOB";
            default:
                return String.format("varchar(%s)", Integer.valueOf(aVar.getColumnSize()));
        }
    }

    @Override
    public String getReNameFieldName(ColumnMeta columnMeta) {
        return "RENAME COLUMN  " + columnMeta.getOldColumnName() + " TO " + columnMeta.getColumnName() + "";
    }

    @Override
    public String getCommentSql(ColumnMeta columnMeta) {
        return "COMMENT ON COLUMN " + columnMeta.getTableName() + "." + columnMeta.getColumnName() + " IS '" + columnMeta.getComment() + CgformUtil.f229C;
    }

    @Override
    public String getUpdateColumnSql(ColumnMeta cgformcolumnMeta, ColumnMeta datacolumnMeta) {
        return null;
    }

    @Override
    public String getSpecialHandle(ColumnMeta cgformcolumnMeta, ColumnMeta datacolumnMeta) {
        return null;
    }

    @Override
    public String dropIndexs(String indexName, String tableName) {
        return "DROP INDEX " + indexName;
    }

    @Override
    public String countIndex(String indexName, String tableName) {
        return "select count(*) from user_ind_columns where index_name=upper('" + indexName + "')";
    }

    /**
     * 是否是blob、double、int、text、date，如果是返回true
     * @param str
     * @return
     */
    private boolean isGivenColumnType(String str) {
        String[] strArr;
        for (String str2 : new String[]{"blob", ExtendJsonKey.f91e, "double", "int", OnlFormShowType.f320d}) {
            if (str2.equals(str)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void handleUpdateMultiSql(ColumnMeta meta, ColumnMeta config, String tableName, List<String> sqlList) {
        String columnName = config.getColumnName();
        String colunmType = meta.getColunmType();
        String colunmType2 = config.getColunmType();
        if (!(colunmType.equals(colunmType2) && meta.getColumnSize() == config.getColumnSize() && meta.getDecimalDigits() == config.getDecimalDigits()) && (!colunmType.equals(colunmType2) || !isGivenColumnType(colunmType2))) {
            sqlList.add("alter table " + tableName + " alter column " + columnName + " set data type " + getDataTypeByColunmType(config));
        }
        if ("Y".equals(config.getIsNullable()) && !config.getIsNullable().equals(meta.getIsNullable())) {
            sqlList.add(String.format("alter table %s alter column %s drop not null", tableName, columnName));
        }
        if ("N".equals(config.getIsNullable()) && !config.getIsNullable().equals(meta.getIsNullable())) {
            sqlList.add(String.format("alter table %s alter column %s set not null", tableName, columnName));
        }
        String fieldDefault = meta.getFieldDefault();
        String fieldDefault2 = config.getFieldDefault();
        if ((!ConvertUtils.isEmpty(fieldDefault) || !ConvertUtils.isEmpty(fieldDefault2)) && !fieldDefault2.equals(fieldDefault)) {
            if (ConvertUtils.isEmpty(fieldDefault2)) {
                fieldDefault2 = "NULL";
            }
            sqlList.add(String.format("alter table %s alter column %s set default %s", tableName, columnName, fieldDefault2));
        }
        if (!meta.mo1291b(config)) {
            sqlList.add(String.format("COMMENT ON COLUMN %s.%s IS '%s'", tableName, columnName, config.getComment()));
        }
    }
}
